//
//  HMDrawView.m
//  copy
//
//  Created by Apple on 16/9/17.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "HMDrawView.h"
#import "HMBeziePath.h"

@interface HMDrawView ()

@property(nonatomic,weak)IBOutlet UILabel *label;

@property(nonatomic,strong)UIColor *lineColor;

@property(nonatomic,strong)NSMutableArray *pathArray;



@end

@implementation HMDrawView
-(void)setEndAngle:(CGFloat)endAngle{
    
    _endAngle = endAngle;
    
    //设置label的值
    _label.text = [NSString stringWithFormat:@"%0.2f%%",endAngle * 100];
    
    //重绘
      [self setNeedsDisplay];
    
}


-(void)awakeFromNib{
    
    [super awakeFromNib];
    
    //初始化数组
    _pathArray = [NSMutableArray array];
}

- (void)drawRect:(CGRect)rect {
 
    CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
    
    CGFloat radius = 120;
    CGFloat radius1 = 80;
    CGFloat start = - M_PI_2 ;
    CGFloat end =  _endAngle * M_PI * 2;
    
    //绘制第一条线
    HMBeziePath  *path = [HMBeziePath  bezierPathWithArcCenter:center radius:radius startAngle:start endAngle:end - M_PI_2 clockwise:YES];
    
    //设置线的颜色
   
    path.lineColor = [UIColor colorWithRed:1 - self.endAngle green:self.endAngle blue:0 alpha:1 ];
    
    
    //添加到数组
    [_pathArray addObject:path];
    
    //绘制第二条线
    HMBeziePath *path1 = [HMBeziePath  bezierPathWithArcCenter:center radius:radius1 startAngle:start endAngle:end - M_PI_2  clockwise:YES];
    
    //设线的颜色
    path1.lineColor = self.backgroundColor;
    
    //添加到数组
    [_pathArray addObject:path1];
    
    for (HMBeziePath *path in _pathArray) {
        
        [path addLineToPoint:center];
        
        [path.lineColor set];
        
        [path fill];
        
    }
    
    //删除路径
    [_pathArray removeAllObjects];
}



@end
